//
//  KSBLEManager.h
//  ROCOMO
//
//  Created by Kyle Sun on 5/12/16.
//  Copyright © 2016 ROCOMO. All rights reserved.
//

#import <CoreBluetooth/CoreBluetooth.h>

/*
 * E2E3 :
 * 读服务：SEND_SERVICE_UUID_NEW   读特征：RECEIVE_CHAR_UUID_NEW
 * 写服务：SEND_SERVICE_UUID_NEW   写特征：SEND_CHAR_UUID_NEW
 * 旧协议：
 * 读服务：RECEIVE_CHAR_UUID   读特征：RECEIVE_CHAR_UUID
 * 写服务：SEND_SERVICE_UUID   写特征：SEND_CHAR_UUID
 * 新协议：- 同E2E3
 * 读服务：SEND_SERVICE_UUID_NEW   读特征：RECEIVE_CHAR_UUID_NEW
 * 写服务：SEND_SERVICE_UUID_NEW   写特征：SEND_CHAR_UUID_NEW
 */

//蓝牙UUID
#define PERIPHERAL_UUID @"FF12"
#define DEVICE_INFO_SERVICE_UUID @"180A"
#define DEVICE_INFO_CHAR_UUID @"2A23"

//写入服务
#define SEND_SERVICE_UUID @"FFE5"
#define SEND_SERVICE_UUID_NEW @"6e400001-b5a3-f393-e0a9-e50e24dcca9e"

//写入特征值
#define SEND_CHAR_UUID @"FF01"
#define SEND_CHAR_UUID_NEW @"6e400002-b5a3-f393-e0a9-e50e24dcca9e"

//接收服务
#define RECEIVE_SERVICE_UUID @"FF12"
#define RECEIVE_SERVICE_UUID_NEW SEND_SERVICE_UUID_NEW

//接收特征值
#define RECEIVE_CHAR_UUID @"FF02"
#define RECEIVE_CHAR_UUID_NEW @"6e400003-b5a3-f393-e0a9-e50e24dcca9e"

extern NSString * const KSBLEPeripheralInfoPeripheralKey;
extern NSString * const KSBLEPeripheralInfoRSSIKey;
extern NSString * const KSBLEManagerErrorDomain;

/// 状态
typedef NS_ENUM(NSInteger, KSBLEManagerState) {
    KSBLEManagerStateUnavailable, /// 不可用
    KSBLEManagerStateReady,       /// 准备好了
    KSBLEManagerStateIdle,        /// 闲置
    KSBLEManagerStateScanning,    /// 搜索中
    KSBLEManagerStateConnecting,  /// 连接中
    KSBLEManagerStateConnected,   /// 已连接
    KSBLEManagerStateSubscribed,  /// 订阅
};

/// 错误
typedef NS_ENUM(NSInteger, KSBLEManagerErrorCode) {
    KSBLEManagerErrorCodeBluetoothPoweredOff = 0,
    KSBLEManagerErrorCodeBluetoothUnavailable = 1,
    KSBLEManagerErrorCodeConnectionFailed = 2,
    KSBLEManagerErrorCodeAutoDisconnected = 3,
    KSBLEManagerErrorCodeCannotSubscribe = 4,
    KSBLEManagerErrorCodeWrongState = 5,
    KSBLEManagerErrorCodeConnectionTimeout = 6,
    KSBLEManagerErrorCodeSacnTimeout = 7,
};

typedef void(^KSBLEManagerScanCompletionBlock)(NSError *error, NSArray<CBPeripheral *> *peripherals);     /// 搜索成功
typedef void(^KSBLEManagerConnectionSuccessBlock)(CBPeripheral *peripheral, NSString *macAddress); /// 连接成功
typedef void(^KSBLEManagerConnectionFailureBlock)(NSError *error);                     /// 连接失败
typedef void(^KSBLEManagerStateChangeBlock)(KSBLEManagerState state);                  /// 状态改变
typedef void(^KSBLEManagerReceiveDataBlock)(NSData *data);                             /// 收到数据

@protocol KSBLEManagerDelegate;

@interface KSBLEManager : NSObject

@property (nonatomic, strong, readonly) NSMutableArray<CBPeripheral *> *discoveredPeripherals; //!< 所有设备
@property (nonatomic, assign, readonly) KSBLEManagerState state;                               //!< 蓝牙状态
@property (nonatomic, assign) BOOL connected;                                                  //!< 连接与否
@property (nonatomic, copy) KSBLEManagerReceiveDataBlock receiveDataBlock;                     //!< 收到数据回调
@property (nonatomic, copy, readonly) NSString *macAdress;                                     //!< mac 地址

@property (nonatomic, copy) KSBLEManagerScanCompletionBlock didScanPeripherals;                 //!< 扫描设备回调

@property (nonatomic , strong)NSString *uploading;  /// 升级中e系列升级蓝牙版使用

@property (nonatomic, strong) CBPeripheral *connectedPeripheral;

+ (instancetype)sharedManager;

- (void)startScanWithWithDuration:(NSTimeInterval)time completion:(KSBLEManagerScanCompletionBlock)completion;
- (void)startScanWithBlock:(KSBLEManagerScanCompletionBlock)completion;
- (void)stopScan;
- (void)connectPeripheral:(CBPeripheral *)peripheral success:(KSBLEManagerConnectionSuccessBlock)success failure:(KSBLEManagerConnectionFailureBlock)failure;
- (void)cancelConnection:(CBPeripheral *)peripheral;
- (void)disconnect;
- (void)sendData:(NSData *)data;
- (void)addStateChangeBlock:(KSBLEManagerStateChangeBlock)block forKey:(NSString *)key;
- (void)removeStateChangeBlockForKey:(NSString *)key;

@end

